;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Enterprise.DealerDictionary", [])
		.controller("DealerDictionaryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService) {
			var tabOpera = {
				0: "新增",
				1: "编辑"
			};
			$scope.tabFlag = {};
			$scope.detailInfo = {};
			$scope.activeFlag = [true,false,false,false];
			$scope.typeList = [];
			$scope.typePageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.conditionType = {};
			$scope.dataList = [];
			$scope.dataPageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.rootType = {
				isChecked:false
			};
			$scope.rootData = {
				isChecked:false
			};
			$scope.knowChildren = false;
			$scope.conditionData = {};
			$scope.parentType = {};

			var btnPermissionCode = {
				paramClassAdd: window.ActionCode.dealerDictionaryAdd,
				paramClassEdit: window.ActionCode.dealerDictionaryEdit
			};

			$scope.btnPermission = {
				paramClassAdd: true,
				paramClassEdit: true
			};

			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			$scope.changeTab = function (index) {
				$scope.activeFlag[index] = true;
				$scope.submitted = false;
				switch (index){
					case 0:
						$scope.parentType = {};
						$scope.dataList = [];
						$scope.knowChildren = false;
						$scope.conditionData = {};
						$scope.detailInfo = {};
						$scope.dataPageInfo = {
							totalPages: 1,
							totalRows: 0,
							pageSize: 10,
							curPage: 1
						};
						$scope.searchType();
						break;
					case 1:
						$scope.searchData();
						break;
					case 2:
						break;
					default:
						break;
				}
			};
			$scope.getChildData = function (item) {
				$scope.knowChildren = true;
				$scope.parentType = item;
				$scope.changeTab(1);
			};
			$scope.addType = function () {
				$scope.detailInfo = {};
				$scope.tabFlag.type = 0;
				$scope.tabFlag.name = tabOpera[0]+'类型';
				$scope.detailInfo.privated = 0;
				$scope.detailInfo.valid = 0;
				$scope.changeTab(2);
			};
			$scope.addData = function (item) {
				$scope.detailInfo = {};
				$scope.parentType = item;
				$scope.tabFlag.type = 0;
				$scope.tabFlag.name = tabOpera[0]+'参数';
				$scope.detailInfo.pCode = item.code;
				$scope.detailInfo.privated = 0;
				$scope.detailInfo.valid = 1;
				$scope.changeTab(2)
			};
			$scope.editType = function (item) {
				$scope.detailInfo = item;
				$scope.tabFlag.type = 1;
				$scope.tabFlag.name = tabOpera[1]+'类型';
				$scope.changeTab(2);
			};
			$scope.editData = function (item) {
				$scope.detailInfo = item;
				$scope.tabFlag.type = 1;
				$scope.tabFlag.name = tabOpera[1]+'参数';
				$scope.changeTab(2);
			};
			$scope.delType = function (type,item) {
				var ids = [];
				if(type=='single'){
					ids.push(item.id);
				}else{
					angular.forEach($scope.typeList,function (item) {
						if(item.isChecked&&!item.privated){
							ids.push(item.id);
						}
					})
				}
				delFunc(ids);
			};
			$scope.delData = function (type,item) {
				var ids = [];
				if(type=='single'){
					ids.push(item.id);
				}else{
					angular.forEach($scope.dataList,function (item) {
						if(item.isChecked&&!item.privated){
							ids.push(item.id);
						}
					})
				}
				delFunc(ids);
			};
			$scope.searchType = function (curPage) {
				getTypeList(curPage);
			};
			$scope.searchData = function (curPage) {
				getDataList(curPage);
			};
			$scope.saveData = function (isValid) {
				if (!isValid) {
					$scope.submitted = true;
					return;
				}
				var service = "device";
				var reqUrl = "/serviceBasicDataAgent/save";
				if ($scope.tabFlag.type === 1) {
					reqUrl = "/serviceBasicDataAgent/save";
				}
				$scope.detailInfo.pCode = !!$scope.detailInfo.pCode?$scope.detailInfo.pCode:'0';
				var param = angular.copy($scope.detailInfo);
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					ybWidget.ShowToaster('success', '操作成功');
					$scope.cancel();
				})
			};
			$scope.cancel = function () {
				if(!!$scope.parentType.name&&$scope.knowChildren){
					$scope.changeTab(1);
				}else{
					$scope.changeTab(0);
				}
			};

			$scope.changeStateData = function (item) {
				if (!item.isChecked) {
					$scope.rootData.isChecked = false;
				} else {
					if (!checkAllData()) {
						$scope.rootData.isChecked = false;
					} else {
						$scope.rootData.isChecked = true;
					}
				}
			};
			$scope.selectAllData = function () {
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					$scope.dataList[i].isChecked = $scope.rootData.isChecked;
				}
			};

			function checkAllData() {
				var selectAll = false;
				for (var i = 0, len = $scope.dataList.length; i < len; i++) {
					if (!$scope.dataList[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}


			$scope.changeStateType = function (item) {
				if (!item.isChecked) {
					$scope.rootType.isChecked = false;
				} else {
					if (!checkAllType()) {
						$scope.rootType.isChecked = false;
					} else {
						$scope.rootType.isChecked = true;
					}
				}
			};
			$scope.selectAllType = function () {
				for (var i = 0, len = $scope.typeList.length; i < len; i++) {
					$scope.typeList[i].isChecked = $scope.rootType.isChecked;
				}
			};

			function checkAllType() {
				var selectAll = false;
				for (var i = 0, len = $scope.typeList.length; i < len; i++) {
					if (!$scope.typeList[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}
			function getTypeList(curPage) {
				var service = 'device';
				var url = '/serviceBasicDataAgent/list';
				var param = {};
				param.pageSize = $scope.typePageInfo.pageSize;
				param.pageNum = curPage || $scope.typePageInfo.curPage;
				param.filters = {
					name: $scope.conditionType.name,
					pCode:'0'
				};
				ybHttp.Post(service,url,param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
					});
					$scope.typePageInfo.totalRows = resp.value.total;
					$scope.typePageInfo.totalPages = resp.value.pages;
					$scope.typeList = resp.value.list;
				})
			}
			function getDataList(curPage) {
				var service = 'device';
				var url = '/serviceBasicDataAgent/list';
				var param = {};
				param.pageSize = $scope.dataPageInfo.pageSize;
				param.pageNum = curPage || $scope.dataPageInfo.curPage;
				param.filters = {
					name: $scope.conditionData.name,
					pCode: $scope.parentType.code
				};
				ybHttp.Post(service,url,param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
					});
					$scope.dataPageInfo.totalRows = resp.value.total;
					$scope.dataPageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				})
			}
			function delFunc(list) {
				if(list.length == 0){
					ybWidget.ShowMessage("请选择可删除的对象");
					return;
				}
				var service = "device";
				var reqUrl = "/serviceBasicDataAgent/delete";
				var param = {ids:list};
				ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
					ybHttp.Post(service, reqUrl, param.ids).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						if(!!$scope.parentType.name){
							$scope.searchData();
						}else{
							$scope.searchType();
						}

					})
				}, function () {

				})
			}
			(function () {
				getTypeList();
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
	;
})(window, window.angular);